package asCachePool
{
	import asCachePool.interfaces.ICacheInfo;
	
	import flash.net.SharedObject;

	/**
	 * 硬盘缓存框架(配合SharedObject来存)
	 * 
	 * 使用例子如下
	 * 
	 * var rc:RichCache = new RichCache();
	 * 
	 * var obj:TestObj = new TestObj();
	 * // 加载数据要转二进制才能存放到到硬盘缓存
	 * rc.putInCache("test1",obj);
	 * var res:TestObj = rc.getCache("test1") as TestObj;
	 * 
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class DiskCache extends BaseCache
	{
		public static const FLUSH_DISK_SIZE:int = 6 * 1024 * 1024;			//缓存的最大体积6M
		public static const DEFAULT_FILE_PATH:String = "game/cache-files";	//把文件缓存至硬盘的路径
		
		private var _so:SharedObject;	//硬盘缓存变量
		
		private var _tempFilePath:String;	//缓存硬盘路径
		private var _tempFileSize:int;		//缓存硬盘最大体积
		
		public function DiskCache(tempFileSize:int=6291456,tempFilePath:String="game/cache-files")
		{
//			super(size);
			_tempFilePath = tempFilePath;
			_tempFileSize = tempFileSize;
		}
		
		/**
		 * 新建池缓存信息对象
		 * @param keyName
		 * @param body
		 */
		protected function newPoolInf(keyName:String,body:Object):ICacheInfo
		{
			var lCache:ICacheInfo = new CacheInfo();
			lCache.setKeyName(keyName);
			lCache.setBody(body);
			lCache.setUpdateTime((new Date()).getTime()/1000);
			lCache.setCount(0);
//			lCache.setExpired(expired);
//			lCache.version = version;
			return lCache;
		}
		
		/**
		 * 将对象同时缓存至内存和硬盘
		 * @param keyName
		 * @param body
		 * @param version		之所以加版本号是为了控制数据模块更新后的同步问题
		 * @param groupName
		 
		public function putInCache(keyName:String, body:Object, version:int=0):void
		{
//			super.putInCache(keyName,body,version,groupName);
			if(body is DisplayObject){
				trace("硬盘不能缓存显示对象!可以将对象转为二进制形式存入.");
				return;
			}
			try
			{
				_so = SharedObject.getLocal(_tempFilePath+"/"+keyName,"/");
				
				_so.data["data"] = body;
				_so.data["version"] = version;
				_so.flush(_tempFileSize);
			}
			catch(error:Error) 
			{
				trace("电脑不支持SharedObject缓存!硬盘缓存失败");
			}
		}*/
		
		override public function putInCache(keyName:String, body:Object):*
		{
			var lCache:ICacheInfo = newPoolInf(keyName,body);
			putCacheInfo(keyName,lCache);
			return body;
		}
		
		/**
		 * 通过池信息缓存至硬盘
		 * @param keyName
		 * @param pInf
		 */
		override public function putCacheInfo(keyName:String, cacheInf:ICacheInfo):ICacheInfo
		{
			try
			{
				_so = SharedObject.getLocal(_tempFilePath+"/"+keyName,"/");
				_so.data["body"] = cacheInf.getBody();
				
				if(cacheInf.isGSVars && cacheInf.dyVars)
				{
					for (var oName:String in cacheInf.dyVars) 
					{
						var val:Object = cacheInf.dyVars[oName];
						_so.data[oName] = val;
					}
				}
				else
				{
					_so.data["body"] = cacheInf.getBody();
					_so.data["count"] = cacheInf.getCount();
					_so.data["expired"] = cacheInf.getExpired();
					_so.data["groupName"] = cacheInf.getGroupName();
					_so.data["keyName"] = cacheInf.getKeyName();
					_so.data["updateTime"] = cacheInf.getUpdateTime();
					_so.data["version"] = cacheInf.getVersion();
				}
				
				_so.flush(_tempFileSize);
			}
			catch(error:Error) 
			{
				trace("电脑不支持SharedObject缓存!硬盘缓存失败");
			}
			return cacheInf;
		}
		
		/**
		 * 获取加载的二进制缓存的数据,没有则返回null;
		 * @param url
		 * @param version
		 
		public function getCache(keyName:String,version:int=0):*
		{
			var pinf:ICacheInfo = getCacheInfo(keyName);
			if(pinf.getVersion()==version)
				return pinf.getBody();
			else
				return null;
			
			return null;
		}*/
		
		override public function getCache(keyName:String):*
		{
			var pinf:ICacheInfo = getCacheInfo(keyName);
			if(pinf)
				return pinf.getBody();
			else
				return null;
		}
		
		/**
		 * 将获取的缓存信息转成IPoolInfo
		 * @param keyName
		 * @return 
		 
		private function getPoolInf(keyName:String):ICacheInfo
		{
			try
			{
				_so = SharedObject.getLocal(_tempFilePath+"/"+keyName,"/");
				var pinf:ICacheInfo = new CacheInfo(_so.data);
				return pinf;
			}
			catch(error:Error) 
			{
				trace("电脑不支持SharedObject缓存!");
				return null;
			}
			return null;
		}*/
		
		override public function getCacheInfo(keyName:String):ICacheInfo
		{
			try
			{
				_so = SharedObject.getLocal(_tempFilePath+"/"+keyName,"/");
				var pinf:ICacheInfo = new CacheInfo(_so.data);
				return pinf;
			}
			catch(error:Error) 
			{
				trace("电脑不支持SharedObject缓存!");
				return null;
			}
			return null;
		}
		
		/**
		 * 将缓存清除
		 * @param keyName
		 */
		override public function removeCache(keyName:String):void
		{
			try
			{
				_so = SharedObject.getLocal(_tempFilePath+"/"+keyName,"/");
				_so.data["body"] = null;
				_so.clear();
				_so.flush();
			}
			catch(error:Error) 
			{
				trace("电脑不支持SharedObject缓存!");
			}
		}
		
		override public function hasCache(keyName:String):Boolean
		{
			return getCacheInfo(keyName)!=null;
		}
	}
}